package com.ncmem.up7.biz;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.ncmem.utils.ConfigReader;
import com.ncmem.model.FileInf;
import com.ncmem.utils.PathTool;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

public class PathBuilder {

    public PathBuilder(){}

    /**
     * 获取上传路径
     * 格式：
     * 	webapp_name/upload
     * @return
     * @throws IOException
     */
    public String getRoot() throws IOException{
        String root = this.getClass().getResource("/").getPath();
        int pos = root.indexOf("target/");

        //非打包模式：/F:/jsp/springboot/up6/target/classs/
        if(StringUtils.startsWith(root,"/"))
        {
            root = root.substring(1,pos);
        }//打包模式
        else if(StringUtils.startsWith(root,"file:/"))
        {
            root = root.substring("file:/".length(),pos);
        }
        else if(pos!=-1)
        {
            root = root.substring(0,pos);
        }
        //fix(2016-02-29):如果路径中包含空格,getPath会自动转换成%20
        root = root.replace("%20", " ");

        ConfigReader cr = new ConfigReader();
        String pathSvr = cr.m_jp.read("IO.dir");
        pathSvr = pathSvr.trim();//
        pathSvr = pathSvr.replace("{root}", root);
        pathSvr = pathSvr.replaceAll("\\\\", "/");
        return pathSvr;

    }
    public String genFolder(String uid,String id,String nameLoc) throws IOException{return "";}

    /**
     * 生成目录存储路径：/2023/03/03/guid/目录名称
     * @param fd
     * @return
     * @throws IOException
     */
    public String genFolder(FileInf fd) throws IOException
    {
        SimpleDateFormat fmtDD = new SimpleDateFormat("dd");
        SimpleDateFormat fmtMM = new SimpleDateFormat("MM");
        SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");

        Date date = new Date();
        String strDD = fmtDD.format(date);
        String strMM = fmtMM.format(date);
        String strYY = fmtYY.format(date);

        String uuid = fd.id;//取消生成新ID,使用原始文件夹ID

        String path = PathTool.combin(this.getRoot(), strYY);
        path = PathTool.combin(path,strMM);
        path = PathTool.combin(path,strDD);
        path = PathTool.combin(path,uuid);
        path = PathTool.combin(path,fd.nameLoc);
        return path;
    }

    /**
     * 生成文件存储路径：/2023/03/03/guid/文件名称
     * @param uid
     * @param f
     * @return
     * @throws IOException
     */
    public String genFile(String uid,FileInf f) throws IOException{
        String uuid = f.id;//取消生成ID，使用自已的ID

        SimpleDateFormat fmtDD = new SimpleDateFormat("dd");
        SimpleDateFormat fmtMM = new SimpleDateFormat("MM");
        SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");

        Date date = new Date();
        String strDD = fmtDD.format(date);
        String strMM = fmtMM.format(date);
        String strYY = fmtYY.format(date);

        String path =  PathTool.combin(this.getRoot(), strYY);
        path = PathTool.combin(path,strMM);
        path = PathTool.combin(path,strDD);
        path = PathTool.combin(path,uuid);
        path = PathTool.combin(path,f.nameLoc);
        return path;
    }
    public String genFile(String uid,String md5,String nameLoc)throws IOException{return "";}
    /**
     * 相对路径转换成绝对路径
     * 格式：
     * 	/2021/05/28/guid/nameLoc => d:/upload/2021/05/28/guid/nameLoc
     * @return
     * @throws IOException
     */
    public String relToAbs(String path) throws IOException
    {
        String root = this.getRoot();
        root = root.replaceAll("\\\\", "/");
        path = path.replaceAll("\\\\", "/");
        if(path.startsWith("/"))
        {
            path = PathTool.combin(root, path);
        }
        return path.trim();
    }
    /**
     * 将路径转换成相对路径
     * @return
     * @throws IOException
     */
    public String absToRel(String path) throws IOException
    {
        String root = this.getRoot().replaceAll("\\\\", "/");
        path = path.replaceAll("\\\\", "/");
        path = path.replaceAll(root, "");
        return path;
    }
}
